/**
 * Created by 王健 on 2017/8/31.
 */

$(function () {
    var live_data = {
        baseConfig: baseConfig,

        prgoram: {},
        show_host_info_obj: {},
        show_img: true,
        show_video_img: true,
        goods_list: [],
        cart_list: [],
        video_list: [],
        goods_ids: [],
        start_list:[{type:1, index:0},{type:1, index:1},{type:1, index:2},{type:1, index:3},{type:1, index:4}],
        is_all: false,
        is_follow: false,
        show_pengyou_dongtai_images: false,
        good_page_index: 0,
        good_page_count: 0,
        goods_order_list: [],
    };

    function checkAll() {
        live_data.goods_ids.splice(0, live_data.goods_ids.length);
        if (!live_data.is_all) {
            live_data.cart_list.forEach(function (item) {
                if(item.is_active){
                    live_data.goods_ids.push(item.goods_id);
                }

            });
        }
        live_data.is_all = !live_data.is_all;
    }

    function query_goods_comment_by_pro_list_by_one() {
        api.lgoods.query_goods_by_hao_ping_pro_list({page_index:live_data.good_page_index+1}).then(function (data) {
            live_data.good_page_index = data.result.page_index;
            live_data.good_page_count = data.result.page_count;
            live_data.goods_list = data.result.list
            _(live_data.goods_list).each(function (obj) {
                if(obj.comment.file_type==0){

                }else if(obj.comment.file_type==1){
                    try{
                        obj.comment.files_json = JSON.parse(obj.comment.files_json);
                    }catch (e) {

                    }

                }else if(obj.comment.file_type==2){
                    try {
                        obj.comment.video_obj = JSON.parse(obj.comment.videos_json);

                    }catch (e) {

                    }

                }
            })
        });
    }

    window.order_detail = new Vue({
        el: '#play_page',
        data: live_data,
        methods:{
            price_filter: price_filter,
            query_goods_comment_by_pro_list_by_one: query_goods_comment_by_pro_list_by_one,
            timeline_str: function () {
                var t = new Date().getTime();
                t = parseInt(t/1000/60/3);
                return "?t="+t;
            },
            open_pro_user: function (user_id) {
                location.href = './pro_user_goods.html?user_id=' + user_id;
            },

            open_pro_user_goods:function(gid, uid){
                location.href = 'goods.html?goods_id='+gid+'&pro_user_id='+uid;
            },

            get_fengmian:function(img_url1, img_url2){
                if(img_url1){
                    return baseConfig.qn_host + img_url1+'?imageView2/1/w/270/h/270';
                }else if(img_url2){
                    return baseConfig.qn_host + img_url2+'?imageView2/1/w/270/h/270';
                }
            },
            show_big_pengyou_img: function (item, fileurl) {
                // program_data.show_pengyou_dongtai = false;
                live_data.show_pengyou_dongtai_images = true;
                var width = window.innerWidth
                    || document.documentElement.clientWidth
                    || document.body.clientWidth;
                var height = window.innerHeight
                    || document.documentElement.clientHeight
                    || document.body.clientHeight;
                setTimeout(function () {
                    var pswpElement = document.getElementById("all_pengyou_pswp");

                    var items = [];
                    var index = 0;

                    _(item.files_json).each(function (sub, i) {
                        items.push({src: baseConfig.qn_host + sub.fileurl +'?imageView2/0/w/'+width+'/h/'+(width*4/3), w: width, h: (width*4/3)})
                        if (sub.fileurl == fileurl) {
                            index = i;
                        }
                    })

                    var options = {
                        // optionName: 'option value'
                        // for example:
                        mainClass: 'pswp--minimal--dark',
                        index: index, // start at first slide

                    };

                    var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
                    gallery.listen('close', function () {
                        // program_data.show_pengyou_dongtai = true;
                        live_data.show_pengyou_dongtai_images = false;
                    });
                    gallery.init();
                }, 200);

            },
            open_order_info: function (order_id) {

                location.href = 'user_goods_orders.html?goods_order_id=' + order_id;
            },
            go_goods_detail:function (goods_id) {
                window.location.href="goods_friend_comment_list.html?goods_id="+goods_id
            },
            show_video:function(){
                live_data.show_video_img =!live_data.show_video_img;
                setTimeout(function () {
                    document.getElementById("goods_video").play();
                },200)
            },
            pause_video:function(){
                live_data.show_video_img =!live_data.show_video_img;

            },
            cart_pay: function (goods_ids) {
                var cart_list_ids = [];
                $.each(goods_ids, function (index, goods_id) {
                    $.each(live_data.cart_list, function (index, c_goods) {
                        if(c_goods.goods_id == goods_id){
                            cart_list_ids.push(c_goods.id)
                        }
                    })
                })
                api.lorder.create_order({cart_list:cart_list_ids.join(',')}).then(function (data) {
                    var payorder_id = data.result.id;
                    location.href = './pay.html?payorder_id='+payorder_id
                })
            },
            delete_cart:function (c_goods) {
                Magiclamp.mcConfirm("提示", "从购物删除商品？", null, function () {
                    api.lgoods.delete_carts({cart_id_list: c_goods.id}).then(function (data) {
                        Magiclamp.mcAlert("提示", data.message, null, function () {
                            c_goods.is_active = false;
                            live_data.is_all=false;
                            checkAll();
                        });

                    });
                });
            },
            pop_num: function (c_goods) {
                c_goods.num -= 1;
                var goods_id = c_goods.goods_id;
                api.lgoods.update_cart({goods_id: goods_id, num: c_goods.num}).then(function (data) {
                })
            },
            add_num: function (c_goods) {
                c_goods.num += 1;
                var goods_id = c_goods.goods_id;
                api.lgoods.update_cart({goods_id: goods_id, num: c_goods.num}).then(function (data) {
                })
            },
            hide_live_list:function () {
                try{
                    client_storage("current_imusername", live_data.prgoram.host__imusername);
                    client_storage("current_realname", live_data.prgoram.host__realname);
                    client_storage("page", "tab_program");

                    client_storage('lat', live_data.show_host_info_obj.la);
                    client_storage('lng', live_data.show_host_info_obj.lo);
                    client_storage('zoom', 15);

                }catch (error){

                }
                location.href = 'home.html';
            },
            go_chat_list:function(){
                try{
                    client_storage("page", "tab_chat");
                }catch (error){

                }
                location.href = 'home.html';
            },
            checkAll: function () {
                checkAll();
            },
            price_all: function (goods_ids) {
                var prices = 0;
                $.each(goods_ids, function (index, goods_id) {
                    $.each(live_data.cart_list, function (index, c_goods) {
                        if (c_goods.goods_id == goods_id) {
                            prices += c_goods.goods__price * c_goods.num
                        }
                    });
                });
                return live_data.cart_list.prices = prices
            },
            shengyudays: function (plan_start_time,live_status) {
                if (live_status == 7) {
                    return "正在直播";
                } else if (!plan_start_time) {
                    return "等待直播";
                } else {
                    var start_time = moment(plan_start_time);
                    var now = moment();
                    if(start_time.isBefore(now)){

                        return "等待直播";
                    }
                    var days = start_time.diff(now, 'days');
                    start_time = start_time.subtract(days, 'days');
                    var hours = start_time.diff(now, 'hours');
                    start_time = start_time.subtract(hours, 'hours');
                    var minutes = start_time.diff(now, 'minutes');
                    if (days == 0) {
                        return "(剩余：" + now.subtract(hours).hours() + "小时)"
                    } else if (days < 0) {
                        return "等待直播";
                    } else {
                        return "(剩余：" + days + "天" + hours + '小时' + minutes + '分)';
                    }
                }

            },
            testPlay: function () {
                //Magiclamp.mcAlert("提示", "testPlay");
                if(live_data.prgoram.live_status!=7){
                    return;
                }

                if(live_data.show_img){
                    //Magiclamp.mcAlert("提示", "1");
                    if(live_data.type=='android'){
                        var play_background = document.getElementById("play_background");
                        play_background.style.top = '50px';
                        window.test_video.addEventListener("x5videoexitfullscreen", function () {
                            //Magiclamp.mcAlert("提示", "3");
                            var play_background = document.getElementById("play_background");
                            play_background.style.top = '0px';
                            live_data.show_img = true;
                        });
                    }

                    window.test_video.play();
                    // document.getElementById("play_background").style.top="50px";

                }else{
                    //Magiclamp.mcAlert("提示", "2");
                    window.test_video.pause();
                    // play_background.style.top = '0px';
                }


                live_data.show_img = !live_data.show_img;
            },
            create_follow:function () {
                api.sys.create_followstore({goodsstore_id:live_data.goodsstore_id}).then(function (data) {
                    live_data.is_follow = true;
                    Magiclamp.mcAlert("提示", data.message, null, function () {
                    });
                })
            },
            delete_follow:function () {
                api.sys.delete_followstore({goodsstore_id:live_data.goodsstore_id}).then(function (data) {
                    live_data.is_follow = false;
                    Magiclamp.mcAlert("提示", data.message, null, function () {
                    });
                })
            },
            go_videos:function () {
                // api.sys.delete_followstore({goodsstore_id:live_data.goodsstore_id}).then(function (data) {
                //     live_data.is_follow = false;
                //     Magiclamp.mcAlert("提示", data.message, null, function () {
                //         location.href = 'goods.html?goods_id='+goods_id
                //     });
                // })
                location.href = 'goodsstore_videos.html?goodsstore_id='+live_data.goodsstore_id
            },
            page_back: function () {
                baseConfig.page_back();
            }
        }
    });

    live_data.sessionid = GetQueryString('sessionid');
    if (live_data.sessionid) {
        live_data.sessionid_q = "sessionid=" + live_data.sessionid;
        client_storage('sessionid', live_data.sessionid);
    }



    document.getElementsByTagName("BODY")[0].onpageshow = function (event) {
        if(event && !event.persisted){
            return;
        }

        query_goods_comment_by_pro_list_by_one();

    };
    document.getElementsByTagName("BODY")[0].onpageshow();






});
function go_home() {
    location.href = "./home.html"
}
